/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.sharedbills.client.form;

import org.jb.sharedbills.client.stores.EventLoader;
import org.jb.sharedbills.shared.model.Event;

import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.inject.Inject;

public class TitlePanel extends InlineLabel {

	@Inject
	public TitlePanel( EventLoader eventLoader ) {
				
		setText( "Chargement...");
		
	    eventLoader.load( new AsyncCallback<Event>() {
			
			@Override
			public void onSuccess(Event result) {
				event = result;
				setText( result.getName());
			}
			
			@Override
			public void onFailure(Throwable caught) {
				setText("Error");
			}
		});
	    
	    addClickHandler( new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent e) {
				if (event == null)
					return;
				
				EventDialog dlg = new EventDialog();
				dlg.show();
				
			}
		});
	}
	
	public class EventDialog extends Dialog {
		public EventDialog() {
			
			setPlain(true);  
			setSize(400, 80);  
			setHeading("Evenement");  
			setLayout(new FitLayout());  
			setModal(true);
			setResizable(false);
	  
			FormPanel panel = new FormPanel();  
			panel.setBorders(false);  
			panel.setBodyBorder(false);  
			panel.setLabelWidth(55);  
			panel.setPadding(5);  
			panel.setHeaderVisible(false);  
	  
			field = new TextField<String>();  
			field.setFieldLabel("Titre");  
			field.setAllowBlank(false);
			field.setValue( event.getName() );
			field.setSelectOnFocus(true);
			field.addKeyListener( new KeyListener() {

				@Override
				public void componentKeyDown(ComponentEvent event) {
					if (event.getKeyCode() == 13) {
						onButtonPressed( getButtonById(Dialog.OK) );
					}
				}
				
			});
			panel.add(field, new FormData("96%"));   
			add( panel );
			setButtons(Dialog.OKCANCEL);
			setHideOnButtonClick(true);
			setFocusWidget(field);
		}
		
		/*
		 * (non-Javadoc)
		 * @see com.extjs.gxt.ui.client.widget.Dialog#onButtonPressed(com.extjs.gxt.ui.client.widget.button.Button)
		 */
		@Override
		protected void onButtonPressed(Button button) {
			if (Dialog.OK.equals( button.getItemId() )) {
				String value = field.getValue();
				if (value == null || value.isEmpty())
					return;
				
				setText( value );
				event.setName( value );
			}
			super.onButtonPressed(button);
		}

		private TextField<String> field;
	}

	private Event event = null;
}
